<?php
if(!defined('SYSTEM_PATH' ) ) die('Cannot access this page directly or see your configuration file ' ) ;
Loader::includeClass('cookie') ;
/**
*
* @author menTiss
* @desc Session management
* @version 1.0
* @date 23/10/2007
*/
class Session extends Cookie {
	/** Session::__construct()
	*
	* @desc Constructing session object
	* @version 1.0
	* @date 23/10/2007
	*
	* @return void
	*/
	public function __construct() {
		parent::__construct() ;
	}
	/**
	 * Session::init()
	 * 
	 * @return 
	 */
	public function init() {
		@session_cache_expire(SESSION_EXPIRE) ;
		@session_start() ;

		// create hash key and define session system:
		$this->hashKey = md5(SESSION_KEY.SITE_NAME);
		if(isset($_SESSION[$this->hashKey])) {
			// get all sessions data
			foreach($_SESSION[$this->hashKey] as $key => $value) {
				$this->_data[$key] = $value ;
			}
		} else {
			// set default session data
			$_SESSION[$this->hashKey] = array() ;
		}
	}
	/** Session::save()
	*
	* @desc Save all current session value or one when give a name of the object
	* @version 1.0
	* @date 23/10/2007
	*
	* @param string $name
	*
	* @return bool
	*/
	public function save($name = null) {
		if(!is_null($name) && isset($this->_data[$name])) {
			$_SESSION[$this->hashKey][$name] = $this->_data[$name] ;
			return true;
		}
		if(is_null($name)) {

			unset($_SESSION[$this->hashKey]) ; // clear actual content
			$_SESSION[$this->hashKey] = array() ;

			foreach($this->_data as $keySession => $valueSession) {
				$_SESSION[$this->hashKey][$keySession] = $valueSession ;
			}
			return true;
		}
		return false;
	}
	/** Session::close()
	*
	* @desc Close and save session (if param is true) or only write current $_SESSION value
	* @version 1.0
	* @date 23/10/2007
	*
	* @param bool $unset
	*
	* @return void
	*/
	public function close($save = true) {
		if(true === $save)
			$this->save() ;
		@session_write_close() ;
	}
	/** Session::destroy()
	*
	* @desc Destroy current session, if param is true unset the session hash key too
	* @version 1.0
	* @date 23/10/2007
	*
	* @param bool $unset
	*
	* @return void
	*/
	public function destroy($unset = false) {
		if(true === $unset)
			unset($_SESSION[$this->hashKey]) ;
		$this->_data = array() ; // flush data in session
		@session_destroy() ;

	}
}
?>